package com.tw.modules.web;


import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tw.modules.common.ConvertJson;
import com.tw.modules.entity.Notebook;
import com.tw.modules.service.NotebookService;
import com.tw.modules.serviceImpl.NotebookServiceImpl;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author maxiaodong
 * @since 2018-04-05
 */
@RestController
@RequestMapping("/notebook")
public class NotebookController {
	@Autowired
	NotebookService notebookserviceimpl;
	
	/**
	 * 查询一个用户所有记事本(测试通过)
	 * 需求分析：根据手机号插叙一个用户的所有记事本，返回集合
	 * @param uphone
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/select", method = { RequestMethod.GET })
	public List<Notebook> select(@RequestParam("uphone")long uphone){
		List<Notebook> list = notebookserviceimpl.selectAllNoteByPhone(uphone);
		return list;
	}
	
	/**
	 * 修改一个记事本（测试通过）
	 * @param notebook
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.GET })
	public String update(@RequestParam("content")String content,
			@RequestParam("phone")long phone,
			@RequestParam("nid")int nid){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Notebook notebook = new Notebook();
		notebook.setnContent(content);
		notebook.setnPhone(phone);
		notebook.setnId(nid);
		notebook.setnDatetime(df.format(new Date()));
		boolean flag = notebookserviceimpl.updateById(notebook);
		if (flag) {
			return ConvertJson.responseJSON("success");
		}
		return ConvertJson.responseJSON("fail");
	}
	
	/**
	 * 删除一个记事本（测试通过）
	 * @param nid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = { RequestMethod.GET })
	public String delete(@RequestParam("n_id")int nid){
		boolean flag = notebookserviceimpl.deleteById(nid);
		if (flag) {
			return ConvertJson.responseJSON("success");
		}
		return ConvertJson.responseJSON("fail");
	}
	
	/**
	 * 某用户新增一个记事本（测试通过）
	 * @param notebook
	 * @return
	 */
	//TODO 小bug:新增时必须指定nid，不然报错会报错
	@ResponseBody	
	@RequestMapping(value = "/insert", method = { RequestMethod.GET })
	public String insert(@RequestParam("content")String content,
			@RequestParam("phone")long phone){
	    int maxId = notebookserviceimpl.selectMaxID();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Notebook notebook  = new Notebook();
		notebook.setnId(maxId+1);
		notebook.setnContent(content);
		notebook.setnPhone(phone);
		notebook.setnDatetime(df.format(new Date()));
		boolean flag = notebookserviceimpl.insert(notebook);
		if (flag) {
			return ConvertJson.responseJSON("success");
		}
		return ConvertJson.responseJSON("fail");
	}

}
